package com.linkit.main.setting;

import java.util.List;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkit.config.Setting;
import com.linkit.main.MainActivity;
import com.linkit.main.R;
import com.linkit.model.Site;
import com.linkit.utils.ScreenUtils;
import com.linkit.utils.Utils;

/**
 * adapter hiện thị danh sách các hãng xe trong tuyến
 * */
public class SitesAdapter extends BaseAdapter implements OnItemClickListener{

	private MainActivity context;
	private List<Site> items;
	private List<String> ids;
	private boolean isSelectedAll;

	public SitesAdapter(List<Site> items) {
		this.context = MainActivity.getInstance();
		this.items = items;
		ids = Utils.parseString(Setting.getSiteId());
		isSelectedAll = Utils.isSelectedAll(ids);
	}

	public class ViewHolder {
		private TextView domain;
		private ImageView imageView;
		private Site site;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		Site rowItem = getItem(position);

		LayoutInflater mInflater = context.getLayoutInflater();
		if (convertView == null) {
			convertView = mInflater.inflate(
					R.layout.setting_site_item, null);
			holder = new ViewHolder();
			holder.domain = (TextView) convertView
					.findViewById(R.id.setting_site_domain);
			
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.setting_site_selected);
			convertView.setTag(holder);
			
		} else{
			holder = (ViewHolder) convertView.getTag();
		}
			

		holder.domain.setText(rowItem.domain);
		holder.domain.setTextSize(ScreenUtils.getDialogItemFontSize());
		holder.site = rowItem;
		
		int resId = R.drawable.checkbox;
		rowItem.isSelected = false;
		
		if (isSelectedAll) {
			resId = R.drawable.checkboxfocus;
			rowItem.isSelected = true;
		} else if (ids != null) {
			int id;
			for (int i = 0; i < ids.size(); i++) {
				id = Integer.parseInt(ids.get(i));
				if (id == holder.site.id) {
					resId = R.drawable.checkboxfocus;
					rowItem.isSelected = true;
					break;
				}
			}

		}
		holder.imageView.setImageResource(resId);
		
		return convertView;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Site getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		ViewHolder holder = (ViewHolder)view.getTag();
		holder.site.isSelected = !holder.site.isSelected;
		
		if(holder.site.isSelected){
			holder.imageView.setImageResource(R.drawable.checkboxfocus);
		}else{
			holder.imageView.setImageResource(R.drawable.checkbox);
		}
		
		// Lưu config
		StringBuffer sBuffer = new StringBuffer("");
		Site site;
		for (int i = 0; i < getCount(); i++) {
			site = getItem(i);
			if (site.isSelected) {
				sBuffer.append(site.id + Setting.SPACE);
			}
		}

		if (sBuffer.length() >= 1) {
			Setting.setSiteId(sBuffer.substring(0, sBuffer.length() - 1));
		} else {
			Setting.setSiteId("");
		}

		Log.e("", "site ===" + Setting.getSiteId());
	}
}